package cn.iocoder.yudao.module.biz.controller.admin.staffPost;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.biz.controller.admin.staffPost.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.staffPost.BizStaffPostDO;
import cn.iocoder.yudao.module.biz.convert.staffPost.BizStaffPostConvert;
import cn.iocoder.yudao.module.biz.service.staffPost.BizStaffPostService;

@Api(tags = "管理后台 - 员工岗位")
@RestController
@RequestMapping("/biz/staff-post")
@Validated
public class BizStaffPostController {

    @Resource
    private BizStaffPostService staffPostService;

    @PostMapping("/create")
    @ApiOperation("创建员工岗位")
    @PreAuthorize("@ss.hasPermission('biz:staff-post:create')")
    public CommonResult<String> createStaffPost(@Valid @RequestBody BizStaffPostCreateReqVO createReqVO) {
        System.out.println(createReqVO);
        return success(staffPostService.createStaffPost(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新员工岗位")
    @PreAuthorize("@ss.hasPermission('biz:staff-post:update')")
    public CommonResult<Boolean> updateStaffPost(@Valid @RequestBody BizStaffPostUpdateReqVO updateReqVO) {
        staffPostService.updateStaffPost(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除员工岗位")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:staff-post:delete')")
    public CommonResult<Boolean> deleteStaffPost(@RequestParam("id") String id) {
        staffPostService.deleteStaffPost(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得员工岗位")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:staff-post:query')")
    public CommonResult<BizStaffPostRespVO> getStaffPost(@RequestParam("id") String id) {
        BizStaffPostDO staffPost = staffPostService.getStaffPost(id);
        return success(BizStaffPostConvert.INSTANCE.convert(staffPost));
    }

    @GetMapping("/list")
    @ApiOperation("获得员工岗位列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('biz:staff-post:query')")
    public CommonResult<List<BizStaffPostRespVO>> getStaffPostList(@RequestParam("ids") Collection<String> ids) {
        List<BizStaffPostDO> list = staffPostService.getStaffPostList(ids);
        return success(BizStaffPostConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/dept-list")
    @ApiOperation("获得部门岗位列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('biz:staff-post:query')")
    public CommonResult<List<BizStaffPostRespVO>> getStaffDeptPostList(@RequestParam("staffDeptId") String staffDeptId) {
        List<BizStaffPostDO> list = staffPostService.getStaffDeptPostList(staffDeptId);
        return success(BizStaffPostConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得员工岗位分页")
    @PreAuthorize("@ss.hasPermission('biz:staff-post:query')")
    public CommonResult<PageResult<BizStaffPostRespVO>> getStaffPostPage(@Valid BizStaffPostPageReqVO pageVO) {
        PageResult<BizStaffPostDO> pageResult = staffPostService.getStaffPostPage(pageVO);
        return success(BizStaffPostConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出员工岗位 Excel")
    @PreAuthorize("@ss.hasPermission('biz:staff-post:export')")
    @OperateLog(type = EXPORT)
    public void exportStaffPostExcel(@Valid BizStaffPostExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BizStaffPostDO> list = staffPostService.getStaffPostList(exportReqVO);
        // 导出 Excel
        List<BizStaffPostExcelVO> datas = BizStaffPostConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "员工岗位.xls", "数据", BizStaffPostExcelVO.class, datas);
    }

}
